package com.code;

import java.util.Map;

public class Result {
	public static final Result defaultResult=new Result();
	public static Result okResult(){
		Result okResult=new Result();
		okResult.setMessage("处理成功");
		return okResult;
	}
	public static Result error(String message){
		Result error=new Result();
		error.error=true;
		error.message=message;
		return error;
	}
	public static Result ok(String message,Object content){
		Result ok=new Result();
		ok.message=message;
		ok.content=content;
		return ok;
	}
	public static Result ok(String message){
		return ok(message,null);
	}
	public Result merge(Result r1){
		if (!r1.isError()){
			if (!isError()){
				message=message+";"+r1.message;
			}
			return this;
		}else{
			if (isError()){
				r1.message=r1.message+";"+message;
			}
			return r1;
		}
		
	}
    private boolean error;
    private boolean waiting;
    public boolean isWaiting() {
		return waiting;
	}
	public void setWaiting(boolean waiting) {
		this.waiting = waiting;
	}
	private String message="";
    private Object content;
	public boolean isError() {
		return error;
	}
	public void setError(boolean error) {
		this.error = error;
	}
	public boolean isSuccess() {
		return !error;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public Object getContent() {
		return content;
	}
	@SuppressWarnings("unchecked")
	public <T> T getContent(Class<T> requiredType) {
		return (T)content;
    }
	@SuppressWarnings("unchecked")
	public Map<String,Object> resultMap(){
		return (Map<String, Object>) content;
	}
	public void setContent(Object content) {
		this.content = content;
	}
	@Override
	public String toString() {
		return (isSuccess()?"成功 ":"失败 ")+message;
	}
	@SuppressWarnings("unchecked")
	public Map<String,Object> contentMap(){
		return (Map<String,Object>)content;
	}
}
